project (FreeOCL)
cmake_minimum_required (VERSION 2.6)

set(FREEOCL_VERSION_HI		0)
set(FREEOCL_VERSION_LO		3)
set(FREEOCL_VERSION_PATCH	2)
set(FREEOCL_VERSION_STRING	"${FREEOCL_VERSION_HI}.${FREEOCL_VERSION_LO}.${FREEOCL_VERSION_PATCH}")
set(FREEOCL_VENDOR	"FreeOCL developers")
set(FREEOCL_MAINTAINER_CONTACT "FreeOCL developers <zuzuf86@gmail.com>")

set(CMAKE_THREAD_PREFER_PTHREAD pthread)
include(FindThreads)
if(NOT CMAKE_THREAD_LIBS_INIT)
	message(FATAL_ERROR "thread library not found")
endif(NOT CMAKE_THREAD_LIBS_INIT)

include(FindOpenMP optional)
if(OPENMP_FOUND)
	message(STATUS "OpenMP found.")
	set(FREEOCL_USE_OPENMP 1)
else(OPENMP_FOUND)
	message(STATUS "OpenMP not found, won't be used.")
	set(FREEOCL_USE_OPENMP 0)
endif(OPENMP_FOUND)

# Default is debug build
if(NOT CMAKE_BUILD_TYPE)
	set(CMAKE_BUILD_TYPE Debug)
endif(NOT CMAKE_BUILD_TYPE)

set(CMAKE_CXX_FLAGS_RELEASE "-O3 -ffast-math")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -ffast-math")

option(FREEOCL_USE_CXX0X "use C++0x" ON)
if(FREEOCL_USE_CXX0X)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -DFREEOCL_USE_CXX0X")
endif(FREEOCL_USE_CXX0X)
if(FREEOCL_USE_OPENMP)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif(FREEOCL_USE_OPENMP)

if(CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFREEOCL_ARCH_i386")
elseif((CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64") OR
	(CMAKE_SYSTEM_PROCESSOR STREQUAL "amd64"))
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFREEOCL_ARCH_amd64")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^alpha")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFREEOCL_ARCH_alpha")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFREEOCL_ARCH_arm")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^mips")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFREEOCL_ARCH_mips")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)64")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFREEOCL_ARCH_ppc64")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFREEOCL_ARCH_ppc")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^sparc")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFREEOCL_ARCH_sparc")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(parisc|hppa)")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFREEOCL_ARCH_parisc")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^s390")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFREEOCL_ARCH_s390")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^sh")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFREEOCL_ARCH_sh")
endif(CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")

option(FREEOCL_RUN_GDB_ON_CRASH "run gdb on crash")
configure_file(config.h.cmake ${FreeOCL_SOURCE_DIR}/include/FreeOCL/config.h)

include_directories(.)
include_directories(include)
include_directories(src)
link_libraries(${CMAKE_THREAD_LIBS_INIT})
find_library(LIBDL dl)
find_library(LIBRT rt)
if(LIBDL)
	link_libraries(${LIBDL})
endif(LIBDL)
if(LIBRT)
	link_libraries(${LIBRT})
endif(LIBRT)

add_subdirectory(samples)
add_subdirectory(src)

install(FILES icd/freeocl.icd DESTINATION /etc/OpenCL/vendors/)
# OpenCL headers shouldn't be needed in the runtime package
# install(DIRECTORY include/CL/ DESTINATION include/CL/ FILES_MATCHING PATTERN "*.h")
install(DIRECTORY include/FreeOCL/ DESTINATION include/FreeOCL/ FILES_MATCHING PATTERN "*.h")

set(CPACK_PACKAGE_NAME "FreeOCL")
set(CPACK_GENERATOR "DEB;RPM;TBZ2")
set(CPACK_PACKAGE_VENDOR "${FREEOCL_VENDOR}")
set(CPACK_PACKAGE_VERSION_MAJOR "${FREEOCL_VERSION_HI}")
set(CPACK_PACKAGE_VERSION_MINOR "${FREEOCL_VERSION_LO}")
set(CPACK_PACKAGE_VERSION_PATCH "${FREEOCL_VERSION_PATCH}")
set(CPACK_PACKAGE_VERSION "${FREEOCL_VERSION_STRING}")
set(CPACK_PACKAGE_DESCRIPTION_FILE "${FreeOCL_SOURCE_DIR}/README")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Implementation of OpenCL 1.2 specifications targeting CPU devices.")
set(CPACK_RESOURCE_FILE_LICENSE "${FreeOCL_SOURCE_DIR}/COPYING")
set(CPACK_RESOURCE_FILE_README "${FreeOCL_SOURCE_DIR}/README")
set(CPACK_DEBIAN_PACKAGE_DEPENDS "g++, libatomic-ops-dev")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "${FREEOCL_MAINTAINER_CONTACT}")
set(CPACK_PACKAGE_CONTACT "${FREEOCL_MAINTAINER_CONTACT}")
set(CPACK_DEBIAN_PACKAGE_PRIORITY "important")

include(CPack)
